﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Linq;
using System.Xml.Linq;

public partial class Default9_4 : System.Web.UI.Page
{
    DataClassesDataContext SMSDC = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dlg_tb = new DataTable();
            DataColumn colWork = new DataColumn("CourseID", System.Type.GetType("System.Int32"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("课程名称", System.Type.GetType("System.String"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("考试状态", System.Type.GetType("System.String"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("最后一次考试日期", System.Type.GetType("System.DateTime"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("ExamNum", System.Type.GetType("System.Int32"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("PassQuestionNum", System.Type.GetType("System.Int32"));
            dlg_tb.Columns.Add(colWork);
            colWork = new DataColumn("积分", System.Type.GetType("System.Int32"));
            dlg_tb.Columns.Add(colWork);

            var h = SMSDC.Edu_GetExamedElectiveCoursebyStaffName(Membership.GetUser().ToString());
            foreach (Edu_GetExamedElectiveCoursebyStaffNameResult c in h)
            {
                DataRow row = dlg_tb.NewRow();
                row["CourseID"] = c.CourseID;
                row["课程名称"] = c.CourseName;
                row["考试状态"] = c.ExamState;
                if (c.ExamDate.Equals(null))
                    row["最后一次考试日期"] = DBNull.Value;
                else
                    row["最后一次考试日期"] = Convert.ToDateTime(c.ExamDate).ToShortDateString();

                row["ExamNum"] = c.ExamNum;
                row["PassQuestionNum"] = c.PassQuestionNum;
                row["积分"] = c.Integral;
                dlg_tb.Rows.Add(row);
            }

            var t = SMSDC.Edu_GetNoExamElectiveCouseByStaffName(Membership.GetUser().ToString());
            foreach (Edu_GetNoExamElectiveCouseByStaffNameResult c in t)
            {
                DataRow row = dlg_tb.NewRow();
                row["CourseID"] = c.CourseID;
                row["课程名称"] = c.CourseName;
               
                
                row["考试状态"] = "未考试";
                row["最后一次考试日期"] = DBNull.Value;
                row["ExamNum"] = 0;
                 row["PassQuestionNum"] = c.PassQuestionNum;
                row["积分"] = 0;
                dlg_tb.Rows.Add(row);
            }

            this.UltraWebGrid2.DataSource = dlg_tb;
            this.UltraWebGrid2.DataBind();
            this.UltraWebGrid2.Columns[2].Hidden = true;
            this.UltraWebGrid2.Columns[6].Hidden = true;
            this.UltraWebGrid2.Columns[7].Hidden = true;
           
            this.UltraWebGrid2.Columns[3].Width = 300;
            this.UltraWebGrid2.Columns[4].Width = 100;
            this.UltraWebGrid2.Columns[5].Width = 120;
            this.UltraWebGrid2.Columns[8].Width = 50;
           
        }
        //    if (Membership.GetUser() != null)
        //    {
        //        string strUserName = Membership.GetUser().ToString();

        //        SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);

        //        DataSet ds = new DataSet();
        //        SqlDataAdapter da = new SqlDataAdapter("select RoleName from aspnet_Users,aspnet_Roles,aspnet_UsersInRoles where aspnet_Users.UserId =aspnet_UsersInRoles. UserId and aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId and UserName = '" + strUserName + "'", objConnection);
        //        da.Fill(ds, "StaffinfoRole");
        //        DataTable dt1 = ds.Tables["StaffinfoRole"];


        //        SqlCommand objCommand = new SqlCommand("Users_FindDepartmentId", objConnection);
        //        objCommand.CommandType = CommandType.StoredProcedure;

        //        SqlParameter objParameter = new SqlParameter("@UserName", SqlDbType.NVarChar);
        //        objCommand.Parameters.Add(objParameter);
        //        objParameter.Direction = ParameterDirection.Input;
        //        objParameter.Value = strUserName;

        //        SqlParameter objOutputParameter = new SqlParameter("@DepartmentID", SqlDbType.Int);
        //        objCommand.Parameters.Add(objOutputParameter);
        //        objOutputParameter.Direction = ParameterDirection.Output;

        //        objConnection.Open();

        //        objCommand.ExecuteNonQuery();
        //        Session["UserDepartmentID"] = (Int32)objOutputParameter.Value;

        //        ArrayList al = new ArrayList();
        //        for (int i = 0; i < dt1.Rows.Count; i++)
        //            al.Add(dt1.Rows[i].ItemArray[0]);

        //        Session["Role"] = al;
        //        Session["UserName"] = strUserName;
        //    }
        //}

    }
    public static string GetNavigateUrlStuy(Object item1)
    {
        int nCourseID = (int)System.Web.UI.DataBinder.Eval(item1, "CourseID");
        string result = "~/edulearn.aspx?CourseID=";
        result +=nCourseID;
        return result;
        
    }

    public  string GetNavigateUrlExam(Object item1)
    {
        int nID = (int)System.Web.UI.DataBinder.Eval(item1, "CourseID");
        string result = "~/eduexamquestions.aspx?CourseID=";
        result +=nID;

        //开始计时
         //DataClassesDataContext SMSDC = new DataClassesDataContext();
        //var t = SMSDC.Edu_GetNoExamElectiveCouseByStaffName(Membership.GetUser().ToString());
        //int b = 1;
        //var a = SMSDC.Edu_GetDate(1).ToString();
        //Session["BeginTime"] = ;
        return result;
    }

    public static bool GetEnabled(Object item1,Object item2,Object item3)
    {
        int status = 0;
        string bExamPass = System.Web.UI.DataBinder.Eval(item1, "考试状态").ToString();
        int nExamNum = (int)System.Web.UI.DataBinder.Eval(item2, "ExamNum");
        if (System.Web.UI.DataBinder.Eval(item3, "PassQuestionNum") == DBNull.Value)
            return status > 0;
        else
        {
            if (bExamPass.ToString().Trim() == "已通过" || nExamNum >= 2)
                return status > 0;
            else
                return status <= 0;
        }
      
    }
}
